package com.cgs.query.service.jpa

import com.cgs.query.domain.QueryDefine
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query

interface QueryDefineRepository : JpaRepository<QueryDefine, String> {
    fun findByName(name: String): List<QueryDefine>?

    fun findByNameLike(name: String, pageable: Pageable): Page<QueryDefine>?

    @Query("SELECT define.* FROM project_tag qt,query_tag tag,define_tag dt,query_define define WHERE tag.NAME = ?2 AND qt.tag_id = tag.id " +
            "AND qt.project_name = ?3 AND dt.tag_id = tag.id AND dt.define_id = define.id AND define.NAME = ?1", nativeQuery = true)
    fun findByNameAndTagNameAndProjectName(name: String, tagName: String, projectName: String): QueryDefine?

    fun findByTags(tags: String, pageable: Pageable): Page<QueryDefine>?

    fun findByTags(tags: String): List<QueryDefine>?

    fun findByTagsAndNameLike(tags: String, name: String): List<QueryDefine>?

    fun findByTagsAndName(tags: String, name: String): ArrayList<QueryDefine>?

    @Query("SELECT qd FROM QueryDefine qd,Project2Define pd WHERE pd.projectName = ?1 AND qd.id = pd.defineId ")
    fun findByProjectName(projectName: String): List<QueryDefine>?

}